package jmine.tec.web.front.dispatcher.request;

import static jmine.tec.web.front.FrontMessages.IO_EXCEPTION_SERIALIZING_FORM;

import java.io.IOException;

import jmine.tec.rpc.common.serialization.Serializer;
import jmine.tec.web.front.FrontException;
import jmine.tec.web.front.domain.BackOfficeRequest;

/**
 * Fills a back office command form content
 * 
 * @author lundberg
 * @param <F> form type
 */
public class BackOfficeRequestContentFiller<F> implements BackOfficeRequestFiller<F> {

    private final Serializer serializer;

    /**
     * Constructor
     * 
     * @param serializer serializer
     */
    public BackOfficeRequestContentFiller(Serializer serializer) {
        this.serializer = serializer;
    }

    /**
     * {@inheritDoc}
     */
    public void fill(BackOfficeRequest request, F form) {
        try {
            request.setFormContent(this.serializer.serialize(form));
        } catch (IOException e) {
            throw new FrontException(IO_EXCEPTION_SERIALIZING_FORM.create(), e);
        }
    }
}
